<?php

class ModelToolSeoUrl extends Model {

    public function rewrite($link) {
        if ($this->config->get('config_seo_url')) {
            $url_data = parse_url(str_replace('&amp;', '&', $link));

            $url = '/';

            $data = array();

            parse_str($url_data['query'], $data);

            foreach ($data as $key => $value) {
                if (($key == 'product_id') || ($key == 'information_id') || ($key == 'news_id')) {
                    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this->db->escape($key . '=' . (int) $value) . "'");

                    if ($query->num_rows) {
                        $url .= $query->row['keyword'];

                        unset($data[$key]);
                    }
                } elseif (($key == 'manufacturer_id')) {
                    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this->db->escape($key . '=' . (int) $value) . "'");

                    if ($query->num_rows) {
                        $url .= $query->row['keyword'] . '/';

                        unset($data[$key]);
                    }
                } elseif ($key == 'path') {
                    $categories = explode('_', $value);

                    foreach ($categories as $category) {
                        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = 'category_id=" . (int) $category . "'");

                        if ($query->num_rows) {
                            $url .= $query->row['keyword'] . '/';
                        }
                    }

                    unset($data[$key]);
                } elseif ($key == 'path_news') {
                    $categories_news = explode('_', $value);

                    foreach ($categories_news as $category_news) {
                        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = 'category_news_id=" . (int) $category_news . "'");

                        if ($query->num_rows) {
                            $url .= $query->row['keyword'] . '/';
                        }
                    }
                    unset($data[$key]);
                } elseif ($key == 'search') {
                    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = 'search=1'");

                    if ($query->num_rows) {
                        $url .= $query->row['keyword'];
                    }
                    unset($data[$key]);
                } elseif ($key == 'sanpham') {
                    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = 'sanpham=1'");

                    if ($query->num_rows) {
                        $url .= $query->row['keyword'] . '/';
                    }
                    unset($data[$key]);
                } elseif ($key == 'contact') {
                    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = 'contact=1'");

                    if ($query->num_rows) {
                        $url .= $query->row['keyword'];
                    }
                    unset($data[$key]);
                }
            }

            if ($url) {
                unset($data['route']);

                $query = '';

                if ($data) {
                    foreach ($data as $key => $value) {
                        $query .= '&' . $key . '=' . $value;
                    }

                    if ($query) {
                        $query = '?' . str_replace('&amp;', '&', trim($query, '&'));
                    }
                }

                return $url_data['scheme'] . '://' . $url_data['host'] . (isset($url_data['port']) ? ':' . $url_data['port'] : '') . str_replace('/index.php', '', $url_data['path']) . $url . $query;
            } else {
                return $link;
            }
        } else {
            return $link;
        }
    }

    public function http($route) {
        return HTTP_SERVER . 'index.php?route=' . str_replace('&', '&amp;', $route);
    }

    public function https($route) {
        if (HTTPS_SERVER != '') {
            $link = HTTPS_SERVER . 'index.php?route=' . str_replace('&', '&amp;', $route);
        } else {
            $link = HTTP_SERVER . 'index.php?route=' . str_replace('&', '&amp;', $route);
        }

        return $link;
    }

    public function khongdau($text) {
        //global $ibforums;
        //Charachters must be in ASCII and certain ones aint allowed
        $text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');
        $text = preg_replace("/(ä|à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", 'a', $text);
        $text = str_replace("ç", "c", $text);
        $text = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $text);
        $text = preg_replace("/(ì|í|î|ị|ỉ|ĩ)/", 'i', $text);
        $text = preg_replace("/(ö|ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", 'o', $text);
        $text = preg_replace("/(ü|ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $text);
        $text = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $text);
        $text = preg_replace("/(đ)/", 'd', $text);
        //CHU HOA
        $text = preg_replace("/(Ä|À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/", 'A', $text);
        $text = str_replace("Ç", "C", $text);
        $text = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", 'E', $text);
        $text = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", 'I', $text);
        $text = preg_replace("/(Ö|Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/", 'O', $text);
        $text = preg_replace("/(Ü|Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", 'U', $text);
        $text = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", 'Y', $text);
        $text = preg_replace("/(Đ)/", 'D', $text);
        $text = preg_replace("/[^a-zA-Z0-9\.\_]/", ' ', $text);
        $text = str_replace("     ", ' ', $text);
        $text = str_replace("    ", ' ', $text);
        $text = str_replace("   ", ' ', $text);
        $text = str_replace("  ", ' ', $text);
        $text = str_replace(" ", '-', trim($text));
        $text = str_replace("---", '-', $text);
        return strtolower($text);
    }

}

?>